package com.studyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.studyadmin.entity.Classes;
import com.studyadmin.entity.Role;
import com.studyadmin.entity.Student;
import com.studyadmin.mapper.ClassesMapper;
import com.studyadmin.service.ClassService;
import com.studyadmin.service.ClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyadmin.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-15
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {


    @Autowired(required = false)
    ClassesMapper classesMapper;
    @Autowired
    ClassServiceImpl classService;
    /**
     * 判断学生是否加入班级
     * @param stuNum
     * @return
     */
    @Override
    public JsonUtil stuClassSer(String stuNum) {

        JsonUtil jsonUtil = new JsonUtil();

        Classes classes = classesMapper.selectOne(
                new QueryWrapper<Classes>()
                        .select("stu_num","cla_num","tea_num")
                        .eq("stu_num",stuNum)
                        .eq("del_flag",0)
        );
        if (classes==null){
            jsonUtil.setCount(0);
            jsonUtil.setMsg("还未加入班级");
        }else {
            jsonUtil.setCount(1);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("加入了班级");
            String claName=classService.selClassName(classes.getClaNum()).getClaName();


            jsonUtil.setData(claName);
        }

        return jsonUtil;
    }

    @Override
    public JsonUtil addClassApplySer(String stuNum, String claNum) {
        JsonUtil jsonUtil = new JsonUtil();
        Classes classes = new Classes();
        classes.setClaNum(claNum);
        classes.setTeaNum("2001");
        classes.setStuNum(stuNum);
        classes.setDelFlag(2);//申请中
        classes.setCreateTime(LocalDateTime.now());
        classes.setUpdateTime(LocalDateTime.now());
        int res=classesMapper.insert(classes);
        if(res<0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("申请失败");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("申请成功");
        }
        return jsonUtil;
    }

    /**
     * 移除学生
     * @param stuNum
     * @return
     */
    @Override
    public JsonUtil delStuClassSer(String stuNum) {
        JsonUtil jsonUtil = new JsonUtil();
        UpdateWrapper<Classes> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("stu_num", stuNum).eq("del_flag",0)
                .set("del_flag",1)
                .set("update_time",LocalDateTime.now());
        int res=classesMapper.update(null, updateWrapper);
        if(res<0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("删除失败");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("移除成功");
        }
        return jsonUtil;
    }
}
